/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cetc.iframe;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.table.DefaultTableModel;

import com.cetc.dao.Dao;

public class XiaoShouChaXun extends javax.swing.JInternalFrame {

    /**
     * Creates new form XiaoShouChaXun
     */
    public XiaoShouChaXun() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jFrame1 = new javax.swing.JFrame();
        jMenu3 = new javax.swing.JMenu();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jLabel1 = new javax.swing.JLabel();
        content = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        condition = new javax.swing.JComboBox<>();
        operation = new javax.swing.JComboBox<>();
        queryButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        startDate = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        endDate = new javax.swing.JTextField();
        showAllButton = new javax.swing.JButton();
        selectDate = new javax.swing.JCheckBox();

        jMenu1.setText("jMenu1");

        jMenu2.setText("jMenu2");

        jMenuItem1.setText("jMenuItem1");

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jMenu3.setText("jMenu3");
        
        // 打开销售查询内部窗体时，设置“起始日期”和“终止日期”
 		addInternalFrameListener(new InternalFrameAdapter() {
 			public void internalFrameActivated(final InternalFrameEvent e) {
 				java.sql.Date date=new java.sql.Date(System.currentTimeMillis());// 创建以系统当前时间为参数的日期对象
 				endDate.setText(date.toString());// 设置“终止日期”文本框中的文本内容
 				date.setMonth(0);// 设置月份为1月
 				date.setDate(1);// 设置日期为当前年份的当前月份的第一天
 				startDate.setText(date.toString());// 设置“起始日期”文本框中的文本内容
 			}
 		});

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("销售信息查询");
        setPreferredSize(new java.awt.Dimension(600, 300));

        jLabel1.setText("选择查询条件：");
        
        content.addKeyListener(new KeyAdapter() {// 为“条件内容”文本框添加键盘事件的监听
			public void keyReleased(final KeyEvent e) {
				if(e.getKeyCode()==10) {// 当按下“回车”键时
					queryButton.doClick();// “查询”按钮执行“单击”操作
				}
			}
		});

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                
            },
            new String [] {
            	"销售票号", "商品名称", "商品编号", "规格", "单价", "数量", "金额", "客户全称", "销售日期", "操作员", "经手人", "结算方式"
            }
        ));
        jTable1.setRowHeight(24);
        jScrollPane1.setViewportView(jTable1);
        jTable1.setEnabled(false);// 设置表格模型无法响应用户输入

        condition.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "客户全称", "销售票号" }));

        operation.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "等于", "包含" }));

        queryButton.setText("查询");
        
        queryButton.addActionListener(new QueryAction());// 为“查询”按钮添加动作事件的监听

        jLabel2.setText("从");

        jLabel3.setText("到");
        
        endDate.addKeyListener(content.getKeyListeners()[0]);// “条件内容”文本框获得键盘事件的监听(当按下“回车”键时，“查询”按钮执行“单击”操作)

        showAllButton.setText("显示全部数据");
        
        showAllButton.addActionListener(new ActionListener() {// 为“显示全部数据”按钮添加动作事件的监听
			public void actionPerformed(final ActionEvent e) {
				content.setText("");// 设置“条件内容”文本框为空
				List list=Dao.findForList("select * from v_sellView");// 获得销售信息的集合
				Iterator iterator=list.iterator();// 创建销售信息集合的迭代器
				updateTable(iterator);// 更新表格数据
			}
		});

        selectDate.setText("指定查询日期");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(selectDate)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(startDate, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(condition, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(operation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(content)
                                .addGap(18, 18, 18)
                                .addComponent(queryButton))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(8, 8, 8)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(endDate, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(27, 27, 27)
                                .addComponent(showAllButton)
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(condition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(content, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(operation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(queryButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(startDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(endDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(showAllButton)
                    .addComponent(selectDate))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    // 条件查询
 	private final class QueryAction implements ActionListener {// “查询”按钮添加动作事件的监听
 		public void actionPerformed(final ActionEvent e) {
 			boolean selDate = selectDate.isSelected();// “指定查询日期”复选框是否被选中
 			if(content.getText().equals("")) {// “条件内容”文本框为空
 				JOptionPane.showMessageDialog(getContentPane(), "请输入查询内容！");// 弹出提示框
 				return;
 			}
 			if(selDate) {// “指定查询日期”复选框被选中
 				if(startDate.getText()==null||startDate.getText().equals("")) {// “起始日期”文本框为空
 					JOptionPane.showMessageDialog(getContentPane(), "请输入查询的开始日期！");// 弹出提示框
 					return;
 				}
 				if(endDate.getText()==null||endDate.getText().equals("")) {// “终止日期”文本框为空
 					JOptionPane.showMessageDialog(getContentPane(), "请输入查询的结束日期！");// 弹出提示框
 					return;
 				}
 			}
 			List list=null;// 结果集
 			String con = condition.getSelectedIndex() == 0 ? "khname " : "sellId ";// 判断“条件名称”下拉列表中被选中的是“客户全称”还是“销售票号”
 			int oper = operation.getSelectedIndex();// 获得“条件运算”下拉列表被选中的选项
 			String opstr = oper == 0 ? "= " : "like ";// 根据“条件运算”下拉列表被选中的选项判断应该是直接查询还是模糊查询
 			String cont = content.getText();// 获得“条件内容”文本框中的文本内容
 			list = Dao.findForList("select * from v_sellView where "
 					+ con
 					+ opstr
 					+ (oper == 0 ? "'"+cont+"'" : "'%" + cont + "%'")
 					+ (selDate ? " and xsdate>'" + startDate.getText()
 							+ "' and xsdate<='" + endDate.getText()+" 23:59:59'" : ""));// 执行拼接的SQL语句后，获得的销售信息结果集
 			Iterator iterator = list.iterator();// 创建销售信息结果集的迭代器
 			updateTable(iterator);// 更新表格数据
 		}
 	}
 	
 	// 更新表格数据
 	private void updateTable(Iterator iterator) {
 		DefaultTableModel dftm = (DefaultTableModel)jTable1.getModel();// 获得默认的表格模型
 		int rowCount=dftm.getRowCount();// 获得默认的表格模型中的行数
 		for(int i=0;i<rowCount;i++) {
 			dftm.removeRow(0);// 移除表格模型中的第一行
 		}
 		while(iterator.hasNext()) {// 销售信息的集合中仍有元素可以迭代
 			Vector vector=new Vector();// 创建向量
 			List view=(List) iterator.next();// 获得迭代的下一个集合
 			vector.addAll(view);// 将迭代后集合中所有的元素添加到此向量中
 			dftm.addRow(vector);// 向默认的表格模型中添加行数据
 		}
 	}

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(XiaoShouChaXun.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(XiaoShouChaXun.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(XiaoShouChaXun.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(XiaoShouChaXun.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new XiaoShouChaXun().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox condition;
    private javax.swing.JTextField content;
    private javax.swing.JTextField endDate;
    private javax.swing.JButton showAllButton;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JComboBox operation;
    private javax.swing.JButton queryButton;
    private javax.swing.JCheckBox selectDate;
    private javax.swing.JTextField startDate;
    // End of variables declaration//GEN-END:variables
}
